package com.example.demo.test;

/**
 * @author Lucy
 * @create 2024-03-08 10:18
 */

import com.example.demo.service.AuthService;
import com.example.demo.utils.Base64Util;
import com.example.demo.utils.FileUtil;
import com.example.demo.utils.HttpUtil;
import com.example.demo.utils.SemanticComparisonUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

/**
 * 通用物体和场景识别
 */
public class AdvancedGeneral {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String advancedGeneral() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
        try {
            // 本地文件路径
            String filePath = "C:\\Users\\Lucy\\Desktop\\身份证\\李岩.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String apiResult = HttpUtil.post(url, accessToken, param);

            String specifiedResult = "身份证";

            int recognitionStatus = recognize(apiResult, specifiedResult);

            switch (recognitionStatus) {
                case 1:
                    System.out.println("识别成功");
                    break;
                case 2:
                    System.out.println("识别失败");
                    break;
                case 3:
                    System.out.println("不确定");
                    break;
                default:
                    System.out.println("无法确定识别状态");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int recognize(String jsonResult, String specifiedResult) {
        Gson gson = new Gson();
        JsonObject resultObject = gson.fromJson(jsonResult, JsonObject.class);
        JsonArray resultArray = resultObject.getAsJsonArray("result");

        for (JsonElement resultElement : resultArray) {
            JsonObject result = resultElement.getAsJsonObject();
            String keyword = result.get("keyword").getAsString();
            double score = result.get("score").getAsDouble();

            if (keyword.contains(specifiedResult) || SemanticComparisonUtil.isSimilar(specifiedResult, keyword)) {
                if (score >= 0.7) {
                    return 1; // 识别成功
                } else if (score < 0.2) {
                    return 2; // 识别失败
                } else {
                    return 3; // 不确定
                }
            }
        }

        return 2; // 识别失败（未找到指定的识别结果）
    }


    public static void main(String[] args) {
        AdvancedGeneral.advancedGeneral();
    }
}
